package com.fast.common.mq;

import java.io.Serializable;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.MessagePostProcessor;

public class TopicSender {

	private JmsTemplate jmsTemplate ;
	
	/**
	 * 发一条消息到指定队列（目标）
	 * @param destination 队列名称
	 * @param message 消息内容
	 */
	public void sendTopic(String destination, final Serializable message) {
		this.jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage(message);
			}
		});
	}
	
	/**
	 * 发一条消息到指定队列（目标）
	 * @param destination 队列名称
	 * @param map java.util.Map
	 */
	public void sendMap(String destination, Map<String, Object> map) {
		this.jmsTemplate.convertAndSend(destination, map, new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message message) throws JMSException {
				return message;
			}
		});
	}
	
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
}
